package com.wyl.bluetoothapp.receiver

import android.bluetooth.BluetoothAdapter
import android.bluetooth.BluetoothDevice
import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import com.wyl.bluetoothapp.domain.BlueInfo
import com.wyl.bluetoothapp.domain.BlueState
import com.wyl.bluetoothapp.util.LogUtil
import org.greenrobot.eventbus.EventBus

class BluetoothReceiver() : BroadcastReceiver() {

    override fun onReceive(context: Context, intent: Intent) {
        when (intent.action) {
            // 1.监听蓝牙的开启状态
            BluetoothAdapter.ACTION_STATE_CHANGED -> {
                when (intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, 0)) {
                    BluetoothAdapter.STATE_TURNING_ON -> LogUtil.i("蓝牙正在打开")
                    BluetoothAdapter.STATE_ON -> LogUtil.i("蓝牙已经打开")
                    BluetoothAdapter.STATE_TURNING_OFF -> {
                        LogUtil.i("蓝牙正在关闭")
                    }
                    BluetoothAdapter.STATE_OFF -> LogUtil.i("蓝牙已经关闭")
                }
            }
            // 2.监听蓝牙的连接状态
            BluetoothAdapter.ACTION_CONNECTION_STATE_CHANGED -> {
                LogUtil.i("蓝牙的连接状态发生改变")
                // 不好用, 用下面的两个状态
                /*when (intent.getIntExtra(BluetoothAdapter.EXTRA_CONNECTION_STATE, 0)) {
                    BluetoothAdapter.STATE_CONNECTING -> LogUtil.i("blueconState: STATE_CONNECTING")
                    BluetoothAdapter.STATE_CONNECTED -> LogUtil.i("blueconState: STATE_CONNECTED")
                    BluetoothAdapter.STATE_DISCONNECTING -> LogUtil.i("blueconState: STATE_DISCONNECTING")
                    BluetoothAdapter.STATE_DISCONNECTED -> LogUtil.i("blueconState: STATE_DISCONNECTED")
                }*/
            }
            BluetoothDevice.ACTION_ACL_CONNECTED -> {
                LogUtil.i("蓝牙设备已连接")
            }
            BluetoothDevice.ACTION_ACL_DISCONNECTED -> {
                LogUtil.i("蓝牙设备已断开")
            }

            // 3.监听蓝牙扫描状态
            //扫描开始
            BluetoothAdapter.ACTION_DISCOVERY_STARTED -> {
                LogUtil.i("扫描开始")
                EventBus.getDefault().post(
                    BlueInfo(
                        BlueState.ACTION_DISCOVERY_STARTED
                    )
                )
            }
            //扫描结束
            BluetoothAdapter.ACTION_DISCOVERY_FINISHED -> LogUtil.i("扫描结束")
            //配对状态变化广播
            BluetoothDevice.ACTION_BOND_STATE_CHANGED -> {
                intent.getParcelableExtra<BluetoothDevice>(BluetoothDevice.EXTRA_DEVICE)?.apply {
                    when (bondState) {
                        BluetoothDevice.BOND_NONE -> LogUtil.i("--- 配对失败 ---")
                        BluetoothDevice.BOND_BONDING -> LogUtil.i("--- 配对中 ---")
                        BluetoothDevice.BOND_BONDED -> {
                            LogUtil.i("--- 配对成功 ---")
                            EventBus.getDefault().post(
                                BlueInfo(
                                    BlueState.BOND_BONDED,
                                    address
                                )
                            )
                        }
                    }
                }
            }
            //发现蓝牙
            BluetoothDevice.ACTION_FOUND -> {
                intent.getParcelableExtra<BluetoothDevice>(BluetoothDevice.EXTRA_DEVICE)?.apply {
                    LogUtil.i("$name, $address")
                    EventBus.getDefault().post(
                        BlueInfo(
                            BlueState.ACTION_FOUND,
                            address
                        )
                    )
                }
            }
        }
    }
}